/**
 * 
 */
package at.ac.tuwien.swa075.telco.auc.dao;

import java.io.IOException;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.Properties;
import java.util.ResourceBundle;

import at.ac.tuwien.swa075.telco.auc.AucException;

/**
 * A factory class that creates a profile DAO object according the the settings
 * in the property file located at /META-INF/services/dao.properties.
 * 
 * @author Johannes Mayr <johannes.mayr@chello.at>
 */
public class ProfileDAOFactory {

	/**
	 * Holds the singleton instance.
	 */
	private static ProfileDAOFactory instance;

	private Class daoClass;

	/**
	 * Creates or returns the singleton instance.
	 * 
	 * @return The factory.
	 * @throws AucException
	 *             An error occured during instance creation.
	 */
	public static ProfileDAOFactory getInstance() throws AucException {
		if (instance == null) {
			instance = new ProfileDAOFactory();
		}
		return instance;
	}

	/**
	 * Creates a ProfileDaoFactory and initialisies it.
	 * 
	 * @throws AucException
	 *             An error occured during initialisation.
	 */
	private ProfileDAOFactory() throws AucException {
		init();
	}

	/**
	 * Initialises the factory. Reads the factory properties dao.properties
	 * file.
	 * 
	 * @throws AucException
	 *             An error occured while reading the properties.
	 */
	private void init() throws AucException {
		String daoPropertyFilePath = "META-INF/services/dao.properties";
		InputStream inStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(daoPropertyFilePath);
		Properties props = new Properties();
		String className = null;
		try {
			props.load(inStream);
			className = props.getProperty("at.ac.tuwien.swa075.telco.auc.dao.implementation");
			daoClass = Thread.currentThread().getContextClassLoader().loadClass(className);
		} catch (IOException e) {
			ResourceBundle bundle = ResourceBundle.getBundle("messages.auc-messages");
			throw new AucException(MessageFormat.format(bundle.getString("AUCE004"), daoPropertyFilePath), e);
		} catch (ClassNotFoundException e) {
			ResourceBundle bundle = ResourceBundle.getBundle("messages.auc-messages");
			throw new AucException(MessageFormat.format(bundle.getString("AUCE005"), className), e);
		}
	}

	public IProfileDAO getProfileDAO() throws AucException {
		try {
			return (IProfileDAO) daoClass.newInstance();
		} catch (InstantiationException e) {
			ResourceBundle bundle = ResourceBundle.getBundle("messages.auc-messages");
			Object[] messageArguments = {daoClass.getName()};
			throw new AucException(MessageFormat.format(bundle.getString("AUCE006"), messageArguments), e);
		} catch (IllegalAccessException e) {
			ResourceBundle bundle = ResourceBundle.getBundle("messages.auc-messages");
			Object[] messageArguments = {daoClass.getName()};
			throw new AucException(MessageFormat.format(bundle.getString("AUCE006"), messageArguments), e);
		}
	}
}
